#include <REGX52.H>
#include "key.h"
#include "delay.h"

static unsigned char Key_Key_Flag = 0;


/**
  * @brief  获取独立按键键码
  * @param  无
  * @retval 按下按键的键码，范围0~4，无按键按下时返回0
  */
unsigned char Key(void)
{
	unsigned char temp = Key_Key_Flag;
	Key_Key_Flag = 0;
	
	return temp;
}

/**
  * @brief  获取当前按下的按键
  * @param  无
  * @retval 按下按键的键码，范围0~4，无按键按下时返回0
  */
unsigned char Scan_Key(void)
{
	unsigned char keyNumber = 0;
	
	if(P3_1 == 0){
		keyNumber = 1;
	}
	
	if(P3_0 == 0){
		keyNumber = 2;
	}
		
	if(P3_2 == 0){
		keyNumber = 3;
	}
	
	if(P3_3 == 0){
		keyNumber = 4;
	}
	
	return keyNumber;
}

/**
  * @brief  Timer0定时器独立按键循环扫描函数，在定时器Timer0里调用每20ms调用一次
  * @param  无
  * @retval 无
  */
void Key_Loop(void)
{
	static unsigned char last_key = 0;
	static unsigned char new_key = 0;
	
	last_key = new_key;
	new_key = Scan_Key();
	
	
	if(last_key  && new_key == 0) //按键松开
	{
		Key_Key_Flag = last_key;
	}
}